﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

/// <summary>
/// 敌人HP
/// </summary>
public class EnemyHP : MonoBehaviour {

    public float maxHP = 100;
    public float hp = 100;
    EnemyHpProgress hpProgress;

    public void Init(float val)
    {
        maxHP = val;
        hp = val;
    }

    void Start () {
        RoleManager.Instance.AddEnemy(this);

        InitHPProgress();
    }
    private void OnDestroy()
    {
        RoleManager.Instance.RemoveEnemy(this);
        EnemyFactory.Instance.enemyCount--;
    }

    //初始化血条UI
    void InitHPProgress()
    {
        var progressPrefab = Resources.Load<GameObject>("UI/ProgressHP");
        var hpUI = GameObject.Instantiate(progressPrefab, UIManager.Instance.CanvasTrans);
        //hpUI.transform.SetParent(UIManager.Instance.CanvasTrans, false);//加上false，缩放不改变，作用同上
        hpProgress = hpUI.GetComponent<EnemyHpProgress>();
        hpProgress.Init(transform);//初始化目标
    }
    public void OnHit()
    {
        if(hp>0)
        {
            hp--;
            hpProgress.SetProgress(hp / maxHP);
            if (hp<=0)
            {
                Destroy(gameObject);
            }
        }

    }
}
